package org.vidyakiran.app.server.service;

import org.vidyakiran.app.client.SecurityService;
import org.vidyakiran.app.server.request.AuthenticateUserRequest;
import org.vidyakiran.app.server.request.AuthenticateUserResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class SecurityServiceImpl extends RemoteServiceServlet implements
SecurityService{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public AuthenticateUserResponse authenticateGoogleAccount(AuthenticateUserRequest request){
		UserService userService = UserServiceFactory.getUserService();
		AuthenticateUserResponse response = new AuthenticateUserResponse();
		
		if(!userService.isUserLoggedIn()){
			response.setLoginUrl(userService.createLoginURL(request.getRedirectUrl(), request.getAuthDomain()));
			response.setLogoutUrl(userService.createLogoutURL(request.getRedirectUrl(), request.getAuthDomain()));
		}else{
			response.setLogoutUrl(userService.createLogoutURL(request.getRedirectUrl(), request.getAuthDomain()));
			response.setAlreadyLoggedIn(true);
		}
		return response;
	}

}
